package org.compiere.process;

import java.awt.Container;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

import org.compiere.apps.IProcessParameter;
import org.compiere.apps.ProcessCtl;
import org.compiere.apps.ProcessParameterPanel;
import org.compiere.apps.Waiting;
import org.compiere.util.ASyncProcess;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;

public class CMS_AgreementList extends SvrProcess implements org.compiere.util.ASyncProcess {

	private BigDecimal CMS_Contract_ID;
	private BigDecimal CMS_Agreement_ID;
	private int AD_Process_ID = 0;
	
	private ASyncProcess m_parent;
	
	@Override
	protected void prepare() {
//		X_cms_agreement agreement = new X_cms_agreement(Env.getCtx(), getRecord_ID(), null);
//		Mcmsagreement agreement = new Mcmsagreement(Env.getCtx(), getRecord_ID(), null);
		CMS_Contract_ID = new BigDecimal(getRecord_ID());
		CMS_Agreement_ID = new BigDecimal(DB.getSQLValue(get_TrxName(), "SELECT MAX(CMS_Agreement_ID) FROM CMS_Agreement WHERE CMS_Contract_ID = " + CMS_Contract_ID));	
		
		String getProcess = "SELECT AD_Process_ID FROM AD_Process WHERE LOWER(Value) LIKE 'cms_agreementlist'";
		AD_Process_ID = DB.getSQLValue(get_TrxName(), getProcess);		
	}

	@Override
	protected String doIt() throws Exception {
		if(AD_Process_ID == 0)
			return  "The process can not be found";
		//  Prepare Process
		ProcessInfo pi = new ProcessInfo ("AgreementList process", AD_Process_ID);
		pi.setAD_User_ID (Env.getAD_User_ID(Env.getCtx()));
		pi.setAD_Client_ID(Env.getAD_Client_ID(Env.getCtx()));
		
		List<ProcessInfoParameter> po = new ArrayList<ProcessInfoParameter>();
		po.add(new ProcessInfoParameter("CMS_Contract_ID",CMS_Contract_ID,null,"",""));
		po.add(new ProcessInfoParameter("CMS_Agreement_ID",CMS_Agreement_ID,null,"",""));
		//
		ProcessInfoParameter[] pp = new ProcessInfoParameter[po.size()];
		po.toArray(pp);
		pi.setParameter(pp);
		//	Execute Process
		ProcessParameterPanel pu = new ProcessParameterPanel(0, pi);
		ProcessCtl.process(this, 0, (IProcessParameter) pu, pi, null);
		
		return "View the report";
	}
	
private Waiting         m_waiting;
	
	@Override
	public void lockUI(ProcessInfo pi) {
		
		JFrame frame = Env.getFrame((Container)m_parent);
		m_waiting = new Waiting (frame, Msg.getMsg(Env.getCtx(), "Processing"), false, pi.getEstSeconds());
		
	}

	@Override
	public void unlockUI(ProcessInfo pi) {

		if (m_waiting != null)
			m_waiting.dispose();
		m_waiting = null;
	}

	@Override
	public boolean isUILocked() {
		return false;
	}

	@Override
	public void executeASync(ProcessInfo pi) {
		
	}
}
